package com.petrochina.cloudconfig.controller;


import com.petrochina.cloudconfig.biz.EnvBiz;
import com.petrochina.cloudconfig.biz.PkgBiz;
import com.petrochina.cloudconfig.conts.ErrorConts;
import com.petrochina.cloudconfig.domain.Env;
import com.petrochina.cloudconfig.domain.Error;
import com.petrochina.cloudconfig.domain.Pkg;
import com.petrochina.cloudconfig.domain.SysInfo;
import com.petrochina.cloudconfig.util.PkgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/pkg")
public class PkgController {

    @Autowired
    private PkgBiz pkgBiz;

    @Autowired
    private EnvBiz envBiz;


    /**
     * getDev pkg list
     * @param keyWord
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public List<Pkg> list(@RequestParam(value = "keyWord",required = false) String keyWord){

        return pkgBiz.list(keyWord);
    }

    /**
     * getDev pkg by id or fullname
     * @param response
     * @param keyWord
     */
    @RequestMapping(value = "/{keyWord:.+}",method = RequestMethod.GET)
    public void getPkg(HttpServletResponse response,@PathVariable String keyWord){
        if(PkgUtil.isPkgFullName(keyWord))
            pkgBiz.getPkgByFullName(response,keyWord);
        if(Integer.getInteger(keyWord)!=null)
            pkgBiz.getPkg(response, Integer.parseInt(keyWord));


    }

    /**
     * getDev pkg info by key word
     * @param keyWord
     * @return
     */
    @RequestMapping(value = "/info/{keyWord:.+}",method = RequestMethod.GET)
    public SysInfo getPkgInfo(String keyWord){
        SysInfo sysInfo = new SysInfo();
        if(PkgUtil.isPkgFullName(keyWord))
            sysInfo.setObj(pkgBiz.getPkgInfoByFullName(keyWord));
        if(Integer.getInteger(keyWord)!=null)
            sysInfo.setObj(pkgBiz.getPkgInfo(Integer.parseInt(keyWord)));

        return  sysInfo;
    }

    /**
     * add a pkg
     * @param pkg
     * @param file
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public SysInfo addPkg(Pkg pkg, @RequestParam("file")MultipartFile file){
        SysInfo sysInfo = new SysInfo();
        //check file is war/jar
        String filename = file.getOriginalFilename();
        String fileType = filename.substring(filename.lastIndexOf(".") + 1);
        if(pkg.getGroupId() != null && pkg.getArtifactId() != null && pkg.getVersionId()!= null){
            if(fileType.equals("war") || fileType.equals("jar")){
                pkg.setPackaging(fileType);
                pkgBiz.addPkg(pkg,file);
                sysInfo.setError(null);
                sysInfo.setObj(true);
            }else{
                Error error = new Error();
                error.setId(ErrorConts.TYPE_NO_NEED_ID);
                error.setId(ErrorConts.TYPE_NO_NEED_MES);
                sysInfo.setError(error);
                sysInfo.setObj(false);
            }
        }else{
            Error error = new Error();
            error.setId(ErrorConts.ITEM_IS_NULL_ID);
            error.setId(ErrorConts.ITEM_IS_NULL_MES);
            sysInfo.setError(error);
            sysInfo.setObj(false);

        }

        return sysInfo;


    }
    @RequestMapping(value = "/{keyWord:.+}",method = RequestMethod.DELETE)
    public SysInfo delPkg(@PathVariable String keyWord){
        //url 中包含.jar的缘故Could not find acceptable representation
        SysInfo sysInfo = new SysInfo();
        if(PkgUtil.isPkgFullName(keyWord))
            sysInfo.setObj(pkgBiz.delPkgByFullName(keyWord));
        if(Integer.getInteger(keyWord)!=null)
            sysInfo.setObj(pkgBiz.delPkg(Integer.parseInt(keyWord)));
        return sysInfo;

    }


    @RequestMapping(value="/{pkgId}/env",method = RequestMethod.GET)
    public List<Env> envList(@PathVariable int pkgId){
        //TODO check file is json
        return envBiz.listByPkgId(pkgId);

    }

    @RequestMapping(value="/{pkgId}/env",method = RequestMethod.POST)
    public SysInfo addEnv(@PathVariable int pkgId, @RequestParam("envName") String envName, @RequestParam("file") MultipartFile file){
        SysInfo sysInfo = new SysInfo();
        //TODO check file is json
        envBiz.addEnv(pkgId,envName,file);

        return sysInfo;

    }


    @RequestMapping(value = "/{pkgId}/env/{envName}",method = RequestMethod.DELETE)
    public SysInfo delEnv(@PathVariable int pkgId,@PathVariable String envName){
        SysInfo sysInfo = new SysInfo();
        envBiz.delEnv(pkgId,envName);
        return sysInfo;
    }





}
